package com.yiwei.zhh.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.ChangePhoneContract;
import com.yiwei.zhh.api.requestInfo.ChangePhoneRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.event.ChangePhoneSuccess;
import com.yiwei.zhh.event.OnHomeEventMsg;
import com.yiwei.zhh.model.ModifyPhoneCheckCodeModel;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.Spans;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.util.Util;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.SendCodeDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by xsooy-pc on 17-11-21.
 */

public class ChangePhoneTotalActivity extends BaseActivity implements ChangePhoneContract.View {
    @BindView(R.id.txt_identity_one)
    TextView txtIdentityOne;
    @BindView(R.id.layout_identity_one)
    LinearLayout layoutIdentityOne;
    @BindView(R.id.txt_identity_two)
    TextView txtIdentityTwo;
    @BindView(R.id.layout_identity_two)
    LinearLayout layoutIdentityTwo;
    @BindView(R.id.btn_call)
    ImageView btnCall;
    private Unbinder unbinder;
    private SendCodeDialog dialog;
    private ChangePhoneRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_change_phone_total_v3);
        baseInitView(this, "ChangePhoneTotalActivity", Const.ViewType.ACTIVITY);
        unbinder = ButterKnife.bind(this);
        initView();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ChangePhoneSuccess msg) {
        int textSize34 = getResources().getDimensionPixelSize(R.dimen.x34);
        int textSize26 = getResources().getDimensionPixelSize(R.dimen.x26);
        Spanned textOne = Spans.with(this).font(Util.replaceString(PreferenceUtils.getString(this, "mobile", ""), 5, 4, '*'), textSize34, 0xff27293E).
                font("\n通过手机验证码验证身份", textSize26, 0xff616185).
                build();
        txtIdentityOne.setText(textOne);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        unbinder.unbind();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        api = ChangePhoneRequest.create();
        api.attachView(this);
        setBackIntent();
        setTitleText("修改手机号");
        int textSize34 = getResources().getDimensionPixelSize(R.dimen.x34);
        int textSize26 = getResources().getDimensionPixelSize(R.dimen.x26);
        Spanned textOne = Spans.with(this).font(Util.replaceString(PreferenceUtils.getString(this, "mobile", ""), 5, 4, '*'), textSize34, 0xff27293E).
                font("\n通过手机验证码验证身份", textSize26, 0xff616185).
                build();
        txtIdentityOne.setText(textOne);

        Spanned textTwo = Spans.with(this).font("人工认证", textSize34, 0xff27293E).
                font("\n通过身份证来认证身份", textSize26, 0xff616185).
                build();
        txtIdentityTwo.setText(textTwo);

        dialog = new SendCodeDialog(this);
        dialog.setContent(PreferenceUtils.getString(this, "mobile", ""));
        dialog.setDialogClickListener(new SendCodeDialog.DialogDefaultClickListener() {
            @Override
            public void confirm(String code) {
                api.getModifyphoneCheckcode(PreferenceUtils.getString(ChangePhoneTotalActivity.this, "mobile", ""), code);
            }

            @Override
            public void cancel() {
                resetBtn();
                dialog.dismiss();
            }

            @Override
            public void sendCode() {
                api.getModifyphoneSendcode(PreferenceUtils.getString(ChangePhoneTotalActivity.this, "mobile", ""));
            }
        });
    }

    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            if (dialog.isShowing()) {
                dialog.setCodeText(paramAnonymousLong / 1000L + "s", false);
            }
        }
    };

    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        if (dialog.isShowing()) {
            dialog.setCodeText("发送", true);
        }
    }

    @OnClick({R.id.layout_identity_one, R.id.layout_identity_two, R.id.btn_call})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.layout_identity_one:
                dialog.show();
                break;

            case R.id.layout_identity_two:
                Util.startActivity(this, ChangePhoneIdcardActivity.class);
                break;
            case R.id.btn_call:
                Intent helpIntent = new Intent(this, HelpCenterActivity.class);
                helpIntent.putExtra("type", 1);
                startActivity(helpIntent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void getModifyphoneSendcode(ResponseModel item) {
        timer.start();
        ToastUtil.makeText(this, "已发送验证码，注意查收").show();
    }

    @Override
    public void getModifyphoneCheckcode(ModifyPhoneCheckCodeModel item) {
        dialog.dismiss();
        Intent msgIntent = new Intent(this, ChangPhoneSecActivity.class);
        msgIntent.putExtra("token", item.token);
        startActivity(msgIntent);
        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
        finish();
    }

    @Override
    public void getModifyphone(ResponseModel item) {

    }

    @Override
    public void getCodeSuccess(ResponseModel item) {

    }
}
